package com.code.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.code.dao.FruitMapper;
import com.code.dao.AddressMapper;
import com.code.entity.PageResult;
import com.code.entity.QueryPageBean;
import com.code.pojo.Fruit;
import com.code.pojo.Address;
import com.code.service.FruitService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class FruitServiceImpl implements FruitService {

    @Autowired
    FruitMapper fruitMapper;
    @Autowired
    AddressMapper productMapper;


    @Override
    public PageResult<Fruit> findPage(QueryPageBean queryPageBean) {
     PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Fruit> fruitList = fruitMapper.findLike(queryPageBean);
        PageInfo<Fruit> pageInfo = new PageInfo<>(fruitList);
        PageResult<Fruit> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;

    }
//    @Override
//    public PageResult<Fruit> findPage(Fruit fruit) {
//     PageHelper.startPage(fruit.getPage(),fruit.getSize());
//        List<Fruit> fruitList = fruitMapper.findLike(fruit);
//        PageInfo<Fruit> pageInfo = new PageInfo<>(fruitList);
//        PageResult<Fruit> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
//        return result;
//    }



    @Override
    public Fruit findById(Integer id) {
        return fruitMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Fruit fruit) {
        fruitMapper.insert(fruit);
    }

    @Override
    public void update(Fruit fruit) {
        fruitMapper.updateByPrimaryKeySelective(fruit);
    }

    @Override
    public void delete(Integer id) {
        fruitMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Address> findAddress() {
        return productMapper.selectAll();
    }


//   统计用注解写Sql语句 字段名称为 name  value
    @Override
    public List<Map> tongji() {
        return fruitMapper.tongji();
    }

//    @Override
//    public List<Map> twobzt() {
//        return fruitMapper.twobzt();
////        return null;
//    }

    @Override
    public PageResult<Fruit> findPageNo(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getPage(),queryPageBean.getSize());
        List<Fruit> fruitList = fruitMapper.findPageNo(queryPageBean);
        PageInfo<Fruit> pageInfo = new PageInfo<>(fruitList);
        PageResult<Fruit> result = new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
        return result;
    }

    @Override
    public void editState(Integer id) {
        Fruit fruit = fruitMapper.selectByPrimaryKey(id);
        fruit.setState(1);
        fruitMapper.updateByPrimaryKeySelective(fruit);
    }
}
